<?php
namespace Admin\Controller;

class PlanCostLimitController extends Controller {

    public function index() {
        $expr = [
            'type'  => 'cost_limit',
            'otype' => 'plan',
        ];

        $r = [];

        if ($record = D2()->where($expr)->select()) {
            foreach ($record as $_row) {
                if (!$_tmp = json_decode($_row['data'], true)) {
                    continue;
                }

                $r[] = [
                    'plan_id' => $_row['oid2'],
                    'tag_id'  => $_tmp['tag_id'],
                    'class'   => $_tmp['class'],
                ];
            }
        }

        $this->assign('record', $r)
             ->display();
    }

    public function doSave() {
        $m = D2();
        $d = I('post.');

        try {
            $r = $m->save2($d) ? array('s' => true, 'm' => '保存成功') : array('s' => false, 'm' => '保存出错');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

}
